package top.readhub.wopei.service.api;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.readhub.wopei.common.api.ISettingApiService;
import top.readhub.wopei.common.constants.DataTypeEnum;
import top.readhub.wopei.service.dal.ISettingConfigDao;
import top.readhub.wopei.service.dal.ISettingRecordDao;
import top.readhub.wopei.service.dal.model.SettingConfigPO;
import top.readhub.wopei.service.dal.model.SettingRecordPO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by daervin on 2021/6/11
 */
@Service
@Component
public class SettingApiServiceImpl implements ISettingApiService {
    @Autowired
    private ISettingConfigDao configDao;
    @Autowired
    private ISettingRecordDao recordDao;

    @Override
    public String get(String code) {
        List<String> records = getRecords(code);
        if (CollectionUtils.isEmpty(records)) {
            return null;
        }
        return records.get(0);
    }

    @Override
    public List<String> gets(String code) {
        return getRecords(code);
    }

    private List<String> getRecords(String code) {
        SettingConfigPO configPO = configDao.selectByCode(code);
        if (configPO == null) {
            return null;
        }
        if (configPO.getDataType() != DataTypeEnum.MULTIPLE_SEARCH.ordinal()) {
            List<SettingRecordPO> recordPOList = recordDao.selectByConfigId(configPO.getId());
            if (CollectionUtils.isEmpty(recordPOList)) {
                return null;
            }
            return recordPOList.stream().map(it -> it.getContent()).collect(Collectors.toList());
        }
        //todo 基于es做检索

        return null;
    }
}
